package com.faithbj.controller.infoInput;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faithbj.common.model.CheckTotal;
import com.faithbj.common.model.Dictionary;
import com.faithbj.common.model.HandleStatus;
import com.faithbj.common.model.Region;
import com.faithbj.common.model.fm.FamilyMember;
import com.faithbj.common.service.RegionService;
import com.faithbj.common.utils.DictionaryUtil;
import com.faithbj.common.web.controller.BaseController;
import com.faithbj.process.ProcessApplicationService;
import com.faithbj.service.CheckTotalService;
import com.faithbj.service.FamilyMemberService;

@Controller
public class IndexController extends BaseController {

    private static final long serialVersionUID = 9068281894134828427L;


    @InitBinder
    @Override
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                dateFormat, false));
    }

    @Resource
    private CheckTotalService checkTotalService;
    @Resource
    private DictionaryUtil dictionaryUtil;
    @Resource
    private RegionService regionService;

    @Resource
    private ProcessApplicationService processApplicationService;
    
    @Resource
    private FamilyMemberService familyMemberService;

    /*	

     @Resource
     private FamilyMemberIncomeService familyMemberIncomeService;*/

    @RequestMapping(value = "/login")
    public String index() {
        return "/common/login";
    }

    @RequestMapping(value = "/index")
    public String main() {
        return "/common/main";
    }

    @RequestMapping(value = "/main")
    public String index(ModelMap map) {
        List<Dictionary> nations = dictionaryUtil.getListByTypeName("民族");
        List<Dictionary> politicalStatus = dictionaryUtil.getListByTypeName("政治面貌");
        List<Dictionary> education = dictionaryUtil.getListByTypeName("文化程度");
        List<Dictionary> laborlost = dictionaryUtil.getListByTypeName("丧劳情况");
        List<Dictionary> disabilityDegree = dictionaryUtil.getListByTypeName("残疾等级");
        List<Dictionary> enclosures = dictionaryUtil.getListByTypeName("基本证明材料");

        List<Region> citys = regionService.getRegionListByType(Region.COUNTRY);
        List<Region> streets = regionService.getRegionListByType(Region.STREET);

        map.put("nations", nations);
        map.put("politicalStatus", politicalStatus);
        map.put("educations", education);
        map.put("laborlost", laborlost);
        map.put("disabilityDegree", disabilityDegree);
        map.put("enclosures", enclosures);
        map.put("citys", citys);
        map.put("streets", streets);
        return "/affairAcceptance/index";
    }

    @RequestMapping(value = "/affairacceptance/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap affairacceptance(CheckTotal checkTotal) {
        ModelMap map = new ModelMap();
        
    	if(null!=familyMemberService.getByIdCardNumber(checkTotal.getFamilyMember().getIdNumber()))
    	{
    		  map.put("status", "3");//申请人身份证号重复
              return map;
    	}

        Set<FamilyMember> familyMembers = new HashSet<FamilyMember>();
        checkTotal.getFamilyMember().setMainApplicant(true);
        checkTotal.getFamilyMember().setRelationMainApplicant("本人");//和主申请人关系，这里01就是本人

        Region regionid = regionService.getRegionByCode(checkTotal.getFamilyMember().getLivingDistrictCode());
        checkTotal.setRegionId(regionid.getId());//给统计用的regionid

        familyMembers.add(checkTotal.getFamilyMember());

        for (FamilyMember fm : familyMembers) {
            fm.setCheckTotal(checkTotal);
        }

        checkTotal.setFamilyMembers(familyMembers);

        //增加受理人和受理时间
        checkTotal.setHandleTime(new Date());
        checkTotal.setHandleUser(getLoginUser().getUsername());
        checkTotal.setBatched(false);//没有生成批次

        checkTotal.setEnclosures();

        String id = checkTotalService.save(checkTotal);

        // xiongyuanwei 启动新流程
        processApplicationService.startAndFinishArriveTask(checkTotal);
        if (checkTotal.getHandleStatus() == HandleStatus.HANDLE) {
            map.put("id", id);
            map.put("status", "1");//跳转到基本信息页
            map.put("tab", "/infoInput/index");
            return map;
        }
        map.put("id", id);
        map.put("status", "2");//跳转到本页面
        map.put("tab", "/affairAcceptance/index");
        return map;
    }


}
